package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.enumerate.UpdateType;
import com.colorlight.cloud.complete.entity.po.Download;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * 更新设备
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class UpdateDevice implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送卡序号
         */
        private Integer prcIndex;
        /**
         * 更新包信息
         */
        private Download download;
        /**
         * 更新类型
         */
        private UpdateType updateType;
    }

    @Data
    @NoArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        // 此处留空，因为 OutputMsg 中没有任何字段
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.UpdateDevice.InputMsg.Builder inputMsg =
                CompleteInstruction.UpdateDevice.InputMsg.newBuilder();
        UpdateDevice.InputMsg thisInput = this.getInput();
        inputMsg.setPrcIndex(thisInput.getPrcIndex());
        inputMsg.setDownload(thisInput.getDownload().toProto());
        inputMsg.setUpdateType(thisInput.getUpdateType().toProto());
        final CompleteInstruction.UpdateDevice.Builder builder =
                CompleteInstruction.UpdateDevice.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public UpdateDevice byteConvertObject(ByteString bytes) {
        this.setOutput(new OutputMsg());
        return this;
    }
}

